// TOWN SCRIPT
//    Town 10: Lighthouse - top (third) floor

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 10, 1 = town dead

begintownscript;

variables;

short i, mnlvl, mxlvl, nlvl, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);

	if (get_flag(10, 1) == 0) {
		mnlvl = 200;
		mxlvl = 0;
		i = 0;
		// get smallest/biggest level in party
		while (i < 4) {
			nlvl = get_level(i);
			if (nlvl > mxlvl)
				mxlvl = nlvl;
			if (nlvl < mnlvl)
				mnlvl = nlvl;
			i = i + 1;
		}
		// balance lich and skeletons level to party's level
		// first, make lich same + 4 of max level (if less than 36)
		if (mxlvl < 36) {
			set_level(6, mxlvl + 4);
		}
		// change level of skeletons if min level in party is < 25
		if (mnlvl < 25) {
			set_level(7, get_level(7) - 4);
			set_level(8, get_level(8) - 4);
		}
	}
	// show first time intro text...
	if (get_flag(10, 0) == 0) {
		set_flag(10, 0, 1);
		reset_dialog();
		add_dialog_str(0, "You make it to the top floor of the lighthouse and see what is keeping the undead here.", 0);
		add_dialog_str(1, "A lich has taken up residence on the top floor.", 0);
		add_dialog_str(2, "It doesn't look happy that you have stumbled into his new domain.", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(1);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;

break;

beginstate 10;
	// stairs down to 2nd floor
	move_to_new_town(9, 21, 12);
	block_entry(1);
break;

beginstate 11;
	// moved to INIT_STATE (intro text)
break;

beginstate 12;
	reset_dialog();
	add_dialog_str(0, "With a final burst of energy, you strike down the lich.", 0);
	add_dialog_str(1, "He disintegrates into dust taking all of his minions with him.", 0);
	add_dialog_str(2, "Strangely enough you feel as if all of the undead in the surrounding region are gone too.", 0);
	add_dialog_choice(0, "OK");
	choice = run_dialog(1);
	set_flag(50, 5, 1);
	if (char_ok(7)) {
		kill_char(7, 2, 0);
	}
	if (char_ok(8)) {
		kill_char(8, 2, 0);
	}
	set_town_status(8, 3);
	set_flag(8, 5, 1);
	set_town_status(9, 3);
	set_flag(9, 2, 1);
	set_town_status(10, 3);
	set_flag(10, 1, 1);
break;

beginstate 13;
	block_entry(1);
	reset_dialog_preset_options(3);
	choice = run_dialog(1);
	if (choice == 2) {
		block_entry(1);
		play_sound(10);
		change_outdoor_location(0, 0, 45, 10);
		move_to_new_town(2, 50, 53);
	}
break;

beginstate 14;
	message_dialog("The portal crackles and hums, beckoning you with safe passage to Cresent Harbor.", "");
break;
